import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbattendance } from '../../../../atoms/db';

const logger = anylogger('pages/api/zllweapp/attendancequery/s003.api');


export type Idata = Pick<ITbattendance, 'coursename' | 'date' | 'teachername' | 'attendanceplace' | 'classname'>[];

export type Idata1 = Pick<ITbattendance, 'coursename' | 'date' | 'teachername' | 'attendanceplace' | 'classname'>[];

export type Result = {
	ok: true;
	data: Idata
	data1: Idata1;
} | {
	ok: false;
	message: string;
};

export type Message1 = {
	Select: string;
	selected: number;
}


/**
 * pages/api/zllweapp/attendancequery/s003.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const { Select, selected } = req.body as Message1;
		if (!Select) {
			res.status(200).json({ ok: false, message: '请选择课程' });
			return;
		}
		const student_id = '201942307789';

		const data = await tbattendance().select('attendanceplace', 'classname', 'coursename', 'date', 'teachername').where({
			coursename: Select
		}).andWhere({
			week: selected
		}).andWhere({ student_id: student_id })
			.andWhere({ attendancestatus: 1 })
			.orderBy('date', 'asc');

		const data1 = await tbattendance().select('attendanceplace', 'classname', 'coursename', 'date', 'teachername').where({
			coursename: Select
		}).andWhere({
			week: selected
		}).andWhere({
			student_id: student_id
		}).andWhere({
			attendancestatus: 2
		}).orderBy('date', 'asc');

		res.status(200).json({ ok: true, data, data1 });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
